#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"

#define NOISY_TEST 0

void testSmithy(){
	int player, preHandCount, handCount;
	int preNumBuys, numBuys;
	int preDeckCount, deckCount;
	int prePlayedCount, playedCount;
	int seed = 50;
	int numPlayer = 3;
	struct gameState state;
	int cur, it;
	int k[10] = {adventurer, council_room, feast, gardens, mine,
					remodel, smithy, village, baron, great_hall};


	// set choices to playDom example
	int choice1 = 0, choice2 = 0, choice3 = 0, handPos = 0, bonus=0;

	for(player = 0; player < numPlayer; player++){
		// clear game state
		//memset(&state, 23, sizeof(struct gameState));

		// initialize game
		int r = initializeGame(numPlayer, k, seed, &state);
		if(r < 0){
         	printf("Error initializing game...\n");
		}

		// set the player
		state.whoseTurn = player;
		// compenstate for initializeGame where other players don't draw their cards
		if(player > 0){
			for(it = 0; it < 5; it++){
             	drawCard(state.whoseTurn, &state);
			}	
		}

		// verify empty hand
		if(state.handCount[player] == 0){
         	state.handCount[player] = 1;
#if (NOISY_TEST == 1)
			printf("Player %d handcount was empty, new handcount: %d\n", player, state.handCount[player]);
#endif
		}

		// force smithy card to be the first card in hand
		state.hand[player][0] = smithy;		

		// Determine preconditions
		preHandCount = state.handCount[player];
		preNumBuys = state.numBuys;
		preDeckCount = state.deckCount[player];
		prePlayedCount = state.playedCardCount; // discard pile

		// Verify 2 cards drawn from Smithy
		int drawCard1 = state.deck[player][preDeckCount - 1];
		int drawCard2 = state.deck[player][preDeckCount - 2];

		cur = cardEffect(smithy, choice1, choice2, choice3, &state, handPos, &bonus);
        if(cur != 0){
        	printf("ERROR: Smithy card could not be used.\n");
			break;
		}                                                        
		// obtain postconditions
		handCount = state.handCount[player];
		numBuys = state.numBuys;
		deckCount = state.deckCount[player];
		playedCount = state.playedCardCount;

		int handCard1 = state.hand[player][handCount - 2];
		int handCard2 = state.hand[player][handCount - 1];
#if (NOISY_TEST == 1)
		printf("Player %d Card #1: Expected: %d. Actual: %d\n", player, drawCard1, handCard1);
#endif
		assert(drawCard1 == handCard1);
#if (NOISY_TEST == 1)
		printf("Player %d Card #2: Expected: %d. Actual: %d\n", player, drawCard2, handCard2);
#endif
		assert(drawCard2 == handCard2);
		
#if (NOISY_TEST == 1)
		printf("Player %d Hand count - Before: %d. Expected: %d. Actual: %d\n", player, preHandCount, preHandCount + 2, handCount);
#endif
		assert(preHandCount + 2 == handCount);
#if (NOISY_TEST == 1)
		printf("Player %d Number of Buys - Before: %d. Expected: %d. Actual: %d\n", player, preNumBuys, preNumBuys, numBuys);
#endif
		assert(preNumBuys == numBuys);
#if (NOISY_TEST == 1)
		printf("Player %d Deck count - Before: %d. Expected: %d. After: %d\n", player, preDeckCount, preDeckCount - 3, deckCount);
#endif
		assert(preDeckCount - 3 == deckCount);
#if (NOISY_TEST == 1)
		printf("Player %d Played count - Before: %d. After: %d\n", player, prePlayedCount, playedCount);
#endif

	}
	printf("Finishing testSmithy()\n");
}

int main(){
	testSmithy();
	return 0;
}
